//SIMMON_ADD
package com.wehealth;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.umeng.analytics.MobclickAgent;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class News_Message extends Fragment {
	
	private static  String JSONResult;
	private TextView news;
	private String newsId;
	private TextView newsTitle ;
	private MainActivity activity ;
	
	public  News_Message()
	{
		Log.i("init:", "Message"); 
	}
	
	public  void setNewsId(String ID){
		newsId = ID;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return  inflater.inflate(R.layout.news_message, null);
	}
	
	
	
	public void onActivityCreated(Bundle savedInstanceState){
		
		super.onActivityCreated(savedInstanceState);

		new Thread(){
			@Override
			public void run(){
				try {
					initDataFromNet();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			handler.sendEmptyMessage(0);
			}
			}.start();
		
		
		
		 activity = (MainActivity)getActivity();
		MobclickAgent.openActivityDurationTrack(false);
		
		//返回主页按钮-------------------------------------------------------//
		 Button bBack = (Button) activity.findViewById(R.id.header_bBack);
			
		 bBack.setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
					
				    activity.switchContentHome();
					
				}
			});
			
		//------------------------------------------------------------------//
		 
	}

	
	private void initDataFromNet() throws IOException{
		 String urlAPI = "http://www.552120.com/API/News_GetNewsInfo.aspx";
		 
		 HttpPost httpRequest= new HttpPost(urlAPI);
		 
		 List <NameValuePair> params = new ArrayList <NameValuePair>();
		 params.add(new BasicNameValuePair("ID",newsId));
		 
		 try{
			 httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8) );
			 HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
			 
			 if(httpResponse.getStatusLine().getStatusCode() == 200)
			 {
				 JSONResult = EntityUtils.toString(httpResponse.getEntity());
			 }
		 } catch(Exception e){
			 Log.e("SIMMON:",  e.toString());
		 }
	
	}

		private Handler handler =new Handler(){
			@Override
			public void handleMessage(Message msg){
			super.handleMessage(msg);
			if(JSONResult !=null){
			    BindNewsData(JSONResult);
			    JSONResult =null;
			}else{
			   // activity.openMainActivityDialog(); 
			}
			}
		};
		
		
		private void BindNewsData(String strJSON){
		    
		    MainActivity activity = (MainActivity)getActivity();
		    View view = (View)activity.findViewById(R.id.header_view);
		    view.setBackgroundResource(R.drawable.bg_redbar);
		    newsTitle = (TextView) activity.findViewById(R.id.header_title);
		    float size =activity.getResources().getDimensionPixelSize(R.dimen.news_title_small_text_size);
		    Log.d("SIMMON","size="+size);
		    newsTitle.setTextSize(size);
		    news = (TextView) activity.findViewById(R.id.news_message);
		    
			try{
					JSONObject jsonObject = new JSONObject(strJSON);
					JSONArray  aNews =  new JSONArray( jsonObject.getString("result") );
	//这里for，将来将所有新闻内容放在一个表格的问题。	    
				    for(int i=0; i<aNews.length(); i++)
				    {
				    		String news_id=aNews.getJSONObject(i).getString("ID");
				    		if(newsId.equals(newsId)){
				    			String title = aNews.getJSONObject(i).getString("title");
				    			newsTitle.setText(title);
				    			String message=aNews.getJSONObject(i).getString("content");
				    			CharSequence richText = Html.fromHtml(message);
				    			news.setText(richText);
				    			return;
				    		}
				    }	    
	    }
	    catch(JSONException ex)
	    {
	    		Log.e("JSON Error: ", ex.toString()); 
	    }
			
		}
	
	    @Override
	    public void onPause() {
	        // TODO Auto-generated method stub
	        super.onPause();
	        MobclickAgent.onPageEnd("News_Message"); 
	    }


	    @Override
	    public void onResume() {
	        // TODO Auto-generated method stub
	        super.onResume();
	        MobclickAgent.onPageStart("News_Message"); 
	    }
	
	

}
